package gui;

import gui.menubar.EditorMenuBar;
import gui.menubar.SaveMapMenuItem;
import gui.toolbar.EditorToolBar;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FileDialog;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.nightpiter.controller.log.Logger;
import com.sun.org.apache.bcel.internal.generic.FCMPG;


import model.MetroModel;
import model.ModelChangeListener;
import model.readers.ReadersRegistry;
import model.readers.MetroModelReaderFactory;
import model.readers.PMetro.Reader;
import model.readers.standard.xml.SaxReader;
import model.readers.standard.xml.XmlReader;
import model.savers.MetroSaversRegistry;
import model.savers.StandardSaver;


public class MainWindow extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	
	private EditorPanel mainPanel; 
	private JMenuBar menu = new JMenuBar();
	private MainWindow frame = this;
	//private final EditorWindowSettings settings = new EditorWindowSettings();
	public static boolean modelChangedAfterLastSave = false;
	 
	
	public MainWindow() {
		super("AndroidMetro editor");
		try  {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e) {
		}
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {			

			@Override
			public void windowClosing(WindowEvent arg0) {
				onClose();
				
			}			
			
		});
			
		this.mainPanel = (new EditorPanel(this));
		this.getContentPane().add(getMainPanel());		
		this.getContentPane().add(new EditorToolBar(this), BorderLayout.NORTH);
		this.setJMenuBar(new EditorMenuBar(this));
		this.pack();
		MetroModel.getInstance().addModelChangeListener(new ModelChangeListener(){

			@Override
			public void modelChanged(boolean newObjectsAdded) {
				modelChangedAfterLastSave = true;
				
			}
			
		});



	}	
	
	public void onClose() {
		if(askForSave("Do you want to quit without saving?",this)!=JOptionPane.CANCEL_OPTION) System.exit(0);
	}
	
	public EditorPanel getMainPanel() {
		return mainPanel;
	}
	
	public static int askForSave(String message, Component parent) {
		if(modelChangedAfterLastSave) {
			int dialogResault = JOptionPane.showConfirmDialog(parent,
					message, "Do you want to save?",
					JOptionPane.YES_NO_CANCEL_OPTION);
			if(dialogResault==JOptionPane.NO_OPTION) {
				SaveMapMenuItem.showSaveFileDialog(parent);
				modelChangedAfterLastSave = false;
			}
			return dialogResault;
			
		}
		return -1;
	}
	
	
	
	
	
}



